const commentModel = require('../mongoModel/commentModel')
const service = {
  // 查找
  async addComment (
    parentId,
    uid,
    address,
    content,
    articleId,
    articleTitle,
    user,
    report
  ) {
    return commentModel.create({
      parentId,
      uid,
      address,
      content,
      articleId,
      articleTitle,
      user,
      report,
      createTime: new Date(),
    }).catch(err => err)
  },

  // 删除多个
  async delAllComment (ids) {
    return commentModel.deleteMany({ _id: ids }).catch((err) => err)
  },
  // 举报
  reportComment (_id) {
    return commentModel.updateOne({ _id }, { report: '2' })
  },
  // 后端查找全部
  async findAllComment (page, limit, where) {
    let count1 = await commentModel.find(where).count()
    let res = await commentModel
      .find(where)
      .sort({ createTime: -1 })
      .skip((page - 1) * limit)
      .limit(limit)
    return { count: Number(count1), data: res }
  },
  // 前端查找
  async findComment (articleId) {
    return commentModel.find({ articleId }).sort({ createTime: -1 })
  },
  // 查找全部数量
  async endfinAll () {
    return { comment: await commentModel.find().count() }
  }
}

module.exports = service
